<?php 
require_once '../functions.php';
require_once '../config.php';
xiu_get_current_user();
header('Content-type:text/html');
$status = array(
  'online' => '在线',
  'offline' => '下线',
  'activated' => '忙碌'
  );
function add_users(){
  GLOBAL $message;
  GLOBAL $success;
  $message='';
  $success=false;
  if(empty($_FILES['avatar'])){
   $message='请上传图片';
   return;
  }else{
    $file=$_FILES['avatar']['tmp_name'];
    $filename=$_FILES['avatar']['name'];
    $path2='../static/uploads/';
    $path3=$path2.$filename;
    $res=move_uploaded_file($file, $path3);
    if(!$res){
      $message='上传失败';
    }
  }

  
  if(empty($_POST['email'])){
    $message='请输入邮箱';
    return;
  }
  if(empty($_POST['slug'])){
    $message='请输入别名';
    return;
  }
  if(empty($_POST['password'])){
    $message='请输入密码';
    return;
  }
  if(empty($_POST['nickname'])){
    $message='请输入昵称';
    return;
  }
  if(empty($_POST['status'])){
    $message='请输入状态';
    return;
  }
  $slug=$_POST['slug'];
  $email=$_POST['email'];
  $password=$_POST['password'];
  $nickname=$_POST['nickname'];
  //$path1=$path3;
  $status=$_POST['status'];
  $row=xiu_execute("insert into users values (null,'{$slug}','{$email}','{$password}','{$nickname}','{$path3}',null,'{$status}')");
  if(!$row){
    $message='插入数据库失败';
    return;
  }else{
    $success='成功'; 
  }
  
}
function edit_users(){
  GLOBAL $message;
  GLOBAL $success;
  GLOBAL $this_use;
  $message='';
  $success=false;
  $id=$this_use['id'];
  // if(empty($_FILES['avatar'])){
  //   //$path1=$this_use['avatar'];
  //   $path1=$path4;
  //   echo '==================';
  //   var_dump($path1);
  // }else{
  //   $file=$_FILES['avatar']['tmp_name'];
  //   $filename=$_FILES['avatar']['name'];
  //   $path2='../static/uploads/';
  //   $path1=$path2.$filename;
  //   $res=move_uploaded_file($file, $path1);
    
  //   $this_use['avatar']=$path1;
  //   if(!$res){
  //     $message='上传失败';
  //   }
  // }
//图片的判断比较严格，看size为空才能判断清除当前是否传入图片
//提交了这个表单，但它的内容为空，上传的路径就为空
//不然上传的一直为空。。。。。。。。。。。。。。。。。。。。。。。。。。。。坑爹呀。。。。。。。。。。。。。。。。。。。
  if(isset($_FILES['avatar']) && $_FILES['avatar']['size']){
    $file=$_FILES['avatar']['tmp_name'];
    $filename=$_FILES['avatar']['name'];
    $path2='../static/uploads/';
    $path1=$path2.$filename;
    $res=move_uploaded_file($file, $path1);
    
    $this_use['avatar']=$path1;
    if(!$res){
      $message='上传失败';
    }
  }else{
    $path1=$this_use['avatar'];
  }

  if(empty($_POST['email'])){
    $email=$this_use['email'];
  }else{
    $email=$_POST['email'];
    $this_use['email']=$_POST['email'];
  }

  if(empty($_POST['slug'])){
      $slug=$this_use['slug'];
    }else{
      $slug=$_POST['slug'];
      $this_use['slug']=$_POST['slug'];
    }

  if(empty($_POST['password'])){
    $password=$this_use['password'];
  }else{
    $password=$_POST['password'];
    $this_use['password']=$_POST['password'];
  }

  if(empty($_POST['nickname'])){
    $nickname=$this_use['nickname'];
  }else{
    $nickname=$_POST['nickname'];
    $this_use['nickname']=$_POST['nickname'];
  }

  if(empty($_POST['status'])){
    $status=$this_use['status'];
  }else{
    $status=$_POST['status'];
    $this_use['status']=$_POST['status'];
  }
    // var_dump($path1);
    // var_dump($email);
    // var_dump($slug);
    // var_dump($nickname);
    // var_dump($password);
    // var_dump($status);


    // $conn=mysqli_connect(XIU_DB_HOST,XIU_DB_USER,XIU_DB_PASS,XIU_DB_NAME);
    // if(!$conn){
    //     //exit('数据库连接失败');
    //   $message='数据库连接失败';
    //   return;
    // }
    // $query=mysqli_query($conn,"update users set avatar = '{$path}',email = '{$email}',slug = '{$slug}',nickname = '{$nickname}',password = '{$password}',status = '{$status}' where id = {$id};");
    // if(!$query){
    //     //查询失败
    //      $message='查询失败';
    //   return;
    // }
    // //对于增删改类的操作都是获取受影响的行数
    // //$rows=mysqli_affected_rows($conn);
    // $rows=mysqli_affected_rows($conn);


  //$rows = xiu_execute("update categories set slug = '{$slug}', name = '{$name}' where id = {$id}");
  $rows=xiu_execute("update users set slug='{$slug}',email='{$email}',password='{$password}',nickname='{$nickname}',avatar='{$path1}',status='{$status}' where id = {$id};");
  if(!$rows){
    $message='插入数据库失败1111';
    return;
  }else{
    $success='成功'; 
  }
}
if(empty($_GET['id'])){
  if($_SERVER['REQUEST_METHOD']=='POST'){
  add_users();
  }
}else{
  $this_use=xiu_fetch_one('select *from users where id= '.$_GET['id']);
  //var_dump($this_use);
  if($_SERVER['REQUEST_METHOD']=='POST'){
    edit_users();
  }
}
$users=xiu_fetch_all('select *from users');
 ?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="utf-8">
  <title>Users &laquo; Admin</title>
  <link rel="stylesheet" href="../static/assets/vendors/bootstrap/css/bootstrap.css">
  <link rel="stylesheet" href="../static/assets/vendors/font-awesome/css/font-awesome.css">
  <link rel="stylesheet" href="../static/assets/vendors/nprogress/nprogress.css">
  <link rel="stylesheet" href="../static/assets/css/admin.css">
  <script src="../static/assets/vendors/nprogress/nprogress.js"></script>
</head>
<body>
  <script>NProgress.start()</script>

  <div class="main">
    <?php include 'inc/navbar.php'; ?>

    <div class="container-fluid">
      <div class="page-title">
        <h1>用户</h1>
      </div>
      <!-- 有错误信息时展示 -->
      <?php if (isset($message)): ?>
        <?php if ($success): ?>
          <div class="alert alert-success">
            <strong>成功！</strong><?php echo $success; ?>
          </div>
        <?php else: ?>
          <div class="alert alert-danger">
            <strong>错误！</strong><?php echo $message; ?>
          </div>
        <?php endif ?>
      <?php endif ?>
      <div class="row">
        <div class="col-md-4">
          <?php if (isset($this_use['id'])): ?>
            <!-- action后面传了参代表修改 -->
              <form action="<?php echo $_SERVER['PHP_SELF']; ?>?id=<?php echo $this_use['id']; ?>" method="post" autocomplete="off" enctype="multipart/form-data">
            <h2>修改<?php echo $this_use['nickname'] ?>信息：</h2>
             <div class="form-group">
              <label for="avatar">上传头像</label>
              <input id="avatar" class="form-control" name="avatar" type="file" placeholder="头像">
            </div>
            <div class="form-group">
              <label for="email">邮箱</label>
              <input id="email" class="form-control" name="email" type="email" placeholder="邮箱" value="<?php echo $this_use['email'] ?>">
            </div>
            <div class="form-group">
              <label for="slug">别名</label>
              <input id="slug" class="form-control" name="slug" type="text" placeholder="slug" value="<?php echo $this_use['slug'] ?>">
              <p class="help-block">https://zce.me/author/<strong>slug</strong></p>
            </div>
            <div class="form-group">
              <label for="nickname">昵称</label>
              <input id="nickname" class="form-control" name="nickname" type="text" placeholder="昵称" value="<?php echo $this_use['nickname'] ?>">
            </div>
            <div class="form-group">
              <label for="password">密码</label>
              <input id="password" class="form-control" name="password" type="text" placeholder="密码" value="<?php echo $this_use['password'] ?>">
            </div>
            <div class="form-group">
              <label for="status">状态</label>
              <input id="status" class="form-control" name="status" type="text" placeholder="昵称" value="<?php echo $this_use['status'] ?>">
            </div>
            <div class="form-group">
              <button class="btn btn-primary" type="submit">添加</button>
            </div>
          </form>
          <?php else: ?>
            <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" autocomplete="off" enctype="multipart/form-data">
            <h2>添加新用户</h2>
             <div class="form-group">
              <label for="avatar">上传头像</label>
              <input id="avatar" class="form-control" name="avatar" type="file" placeholder="头像">
            </div>
            <div class="form-group">
              <label for="email">邮箱</label>
              <input id="email" class="form-control" name="email" type="email" placeholder="邮箱">
            </div>
            <div class="form-group">
              <label for="slug">别名</label>
              <input id="slug" class="form-control" name="slug" type="text" placeholder="slug">
              <p class="help-block">https://zce.me/author/<strong>slug</strong></p>
            </div>
            <div class="form-group">
              <label for="nickname">昵称</label>
              <input id="nickname" class="form-control" name="nickname" type="text" placeholder="昵称">
            </div>
            <div class="form-group">
              <label for="password">密码</label>
              <input id="password" class="form-control" name="password" type="text" placeholder="密码">
            </div>
            <div class="form-group">
              <label for="status">状态</label>
              <input id="status" class="form-control" name="status" type="text" placeholder="昵称">
            </div>
            <div class="form-group">
              <button class="btn btn-primary" type="submit">添加</button>
            </div>
          </form>
            
          <?php endif ?>
        </div>
        <div class="col-md-8">
          <div class="page-action">
            <!-- show when multiple checked -->
            <a class="btn btn-danger btn-sm" href="user-delete.php" style="display: none" id="all">批量删除</a>
          </div>
          <table class="table table-striped table-bordered table-hover">
            <thead>
               <tr>
                <th class="text-center" width="40"><input type="checkbox"></th>
                <th class="text-center" width="80">头像</th>
                <th>邮箱</th>
                <th>别名</th>
                <th>昵称</th>
                <th>状态</th>
                <th class="text-center" width="100">操作</th>
              </tr>
            </thead>
            <tbody>
              <?php foreach ($users as $item): ?>
                <tr>
                <td class="text-center"><input type="checkbox" data-id="<?php echo $item['id']; ?>"></td>
                <td class="text-center"><img class="avatar" src="<?php echo $item['avatar']; ?>"></td>
                <td><?php echo $item['email']; ?></td>
                <td><?php echo $item['slug']; ?></td>
                <td><?php echo $item['nickname']; ?></td>
                <td><?php echo $status[$item['status']]; ?></td>
                <td class="text-center">
                  <a href="users.php?id=<?php echo $item['id']; ?>" class="btn btn-default btn-xs">编辑</a>
                  <a href="user-delete.php?id=<?php echo $item['id']; ?>" class="btn btn-danger btn-xs">删除</a>
                </td>
              </tr>
              <?php endforeach ?>
              
              
            </tbody>
          </table>
        </div>
      </div>
    </div>
  </div>

  <?php $current_page = 'users'; ?>
  <?php include 'inc/sidebar.php'; ?>

  <script src="../static/assets/vendors/jquery/jquery.js"></script>
  <script src="../static/assets/vendors/bootstrap/js/bootstrap.js"></script>
  <script>
  $(function($){
    var $input=$("tbody input");
    var $input1=$("thead input");
    var length=$input.length;
    var arrAll=[];
    var flag=false;
    var $all=$("#all");//删除的按钮
    $input.on('click',function(){
      var id=$(this).attr("data-id");
      if($(this).prop("checked")){
        arrAll.includes(id) || arrAll.push(id);
      }else{
        //没被勾先找到这个id在数组里面的索引
        //再删掉这个索引上的数字
        var id1=arrAll.indexOf(id);
        arrAll.splice(id1, 1);
      }
      arrAll.length ? $all.fadeIn() : $all.fadeOut();
      $all.prop("search","?id="+arrAll);
      if(length==arrAll.length){
        $input1.prop("checked",true);
      }
    });
    $input1.on('click',function(){
      flag=$(this).prop("checked");
      $input.prop("checked",flag);
    });



  });
  </script>
  <script>NProgress.done()</script>
</body>
</html>
